package com.yndsoft.jc.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.List;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.ParseException;
import android.util.Log;
import android.widget.Toast;

import com.yndsoft.jc.R;

public class NetHelper {
	private static final int CONNECTION_TIMEOUT = 5 * 1000;// 设置请求超时5秒钟
	private static final int SO_TIMEOUT = 5 * 1000; // 设置等待数据超时时间5秒钟

	/**
	 * 检测是否有网络
	 *
	 * @param context
	 * @return
	 */
	public static boolean hasNetwork(Context context) {
		ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo workinfo = con.getActiveNetworkInfo();
		if (workinfo == null || !workinfo.isAvailable()) {
			Toast.makeText(context, R.string.error_network, Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	public static void ssss() {
		try {
			Socket client = new Socket("127.0.0.1", 9099); // 连接服务器
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
	}

	// Android4.0以后不能再主线程中打开连接
	public static void testNetThread(final String url) {
		new Thread() {
			public void run() {
				try {
					Socket client = new Socket("192.168.31.146", 9099);
					// 连接服务器
				} catch (Exception e) {

				}
			};
		}.start();
	}
}